# -*- coding: utf-8 -*-
from com.zzh.utils.dbUtils import MysqlUtils
from com.zzh.utils.table2JSON import Table2Json
import time
import random
PAGE_SIZE = 10
local_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))

'''
姚明 281720252@qq.com
'''
class HotSearchAPI(object):
    def __init__(self):
        self.conn = MysqlUtils.get_mysql_connections()#初始化数据库链接

    def queryHotSearch(self,page_index):#zzh_prochild表查询记录,根据什么规则查询？
        cur = self.conn.cursor()
        sql = "select * from zzh_hotsearch"
        total_count = cur.execute(sql)
        hotsearchList = cur.fetchall()
        cur.close()
        self.conn.close()

        if (page_index * PAGE_SIZE) < total_count:
            start = (page_index - 1) * PAGE_SIZE
            end = page_index * PAGE_SIZE
            horsearchListPart = hotsearchList[start: end]

            hotsearchListJson=Table2Json.hotsearch_list_to_json(horsearchListPart)
            return total_count,hotsearchListJson
        else:
            start = (page_index - 1) * PAGE_SIZE
            horsearchListPart = hotsearchList[start:]
            hotsearchListJson=Table2Json.hotsearch_list_to_json(horsearchListPart)
            return  total_count,hotsearchListJson

    def addHotSearch(self, addHotsearch):#zh_hotsearch表添加记录,默认增加的时候，搜索次数为1
        itemId = addHotsearch['itemId']  #itemId  关联zzh_item表item_id字段。
        searchCount = addHotsearch['searchCount']  #searchCount  搜索计数器，好像可以不填，每次搜索的时候进行更新，但是有个疑问，更新规则是？
        hotsearchStatus = addHotsearch['hotsearchStatus']#热搜状态，这个状态背后的含义指的是什么？

        cur = self.conn.cursor()
        sql = "INSERT INTO zzh_hotsearch (item_id,search_count,status) values (%s,%s,%s)"
        addResultOfDatabaseHotsearch = cur.execute(sql,[itemId, searchCount, hotsearchStatus])
        self.conn.commit()
        if addResultOfDatabaseHotsearch:#插入成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#插入失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def updateHotsearchById(self,newHotsearch,hotsearchId):
        itemId = newHotsearch['itemId']  #itemId  关联zzh_item表item_id字段。
        searchCount = newHotsearch['searchCount']  #searchCount  搜索计数器，好像可以不填，每次搜索的时候进行更新，但是有个疑问，更新规则是？
        hotsearchStatus = newHotsearch['status']#热搜状态，这个状态背后的含义指的是什么？

        cur=self.conn.cursor()
        sql="UPDATE zzh_hotsearch SET item_id=%s,search_count=%s,status=%s WHERE hotsearch_id=%s"
        updateHotsearchResultOfDatabase = cur.execute(sql,[itemId, searchCount, hotsearchStatus , hotsearchId])
        self.conn.commit()
        if updateHotsearchResultOfDatabase:#更新成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#更新失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def deleteHotsearchById(self,hotsearchId):
        cur=self.conn.cursor()
        sql="DELETE from zzh_hotsearch WHERE hotsearch_id=%s"
        deleteHotsearchResultOfDatabase = cur.execute(sql,[hotsearchId])
        self.conn.commit()
        if deleteHotsearchResultOfDatabase:#删除成功，关闭数据库链接，返回1
            cur.close()
            self.conn.close()
            return 1
        else:#删除失败，关闭数据库链接，返回0
            cur.close()
            self.conn.close()
            return 0

    def get_random_item(self):
        cur = self.conn.cursor()

        sql = "select item_id from zzh_hotsearch"
        rs = cur.execute(sql)
        ids = cur.fetchall()

        ranInt = random.randint(0, rs - 1)
        item_id = ids[ranInt][0]

        sql2 = "select * from zzh_item WHERE item_id=%s"
        rs = cur.execute(sql2, [item_id])
        data = cur.fetchall()
        json2 = Table2Json.item_list_to_json(data)

        return json2